﻿using Helpers;
using QFramework;
using UnityEngine;

namespace Gp.Scripts.Core
{
    public class HumanoidUnit : BaseUnit
    {
        public override void AfterComponentInit()
        {
            if (this.EntityBehaviour.TryGetComponent<CharacterController>(out var ch))
            {
                ch.radius = .3f;
            }


            // 载入子物体
            // LoadSubObjectProcess();

            // // 添加计数组件
            // var ui = this.GetComponentInChildren<UnitTurnCountDisplayUI>();
            // if (ui != null) {
            //     ui.Init(this);
            // }

            // 将单位放置与寻路网格上
            if (NavHelper.SampleNodePosition(transform.position, out var groundPos))
            {
                transform.position = groundPos + new Vector3(0, 0.25f, 0);
            }


            this.RegisterEvent<Event_Unit.BattleStart>(_ =>
                this.gameObject.layer = LayerEnum.BattleCharacter.ToLayerInt());
            // this.RegisterEvent<Event_Unit.BattleEnd>(_ => this.gameObject.Layer("Default"));
        }


        private void LoadSubObjectProcess()
        {
            ResLoader.LoadByBundleAsync(ConstBundle.UNIT_SUB_OBJ, o =>
            {
                var obj = Object.Instantiate(o, transform, false);
                if (obj.TryGetComponent<UnitSubObject>(out var component))
                    component.Init(this);
            });
        }

    }
}